﻿using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using NDK.Module.Model;
using NDK.Utils;
using System.IO;
using NDK.LocalUtils;

namespace NDK.Frm
{
    public partial class Dlg_PointCheck : Form
    {
        private static log4net.ILog m_Logger = log4net.LogManager.GetLogger("Dlg_PointCheck");
        DataTable dt = new DataTable();
        string tempPath = Common.pointCheckEncrypterPath + @"\temp.bz";
        string filePath = "";
        public Dlg_PointCheck()
        {
            InitializeComponent();
        }

        private void btn_ReadPointCheckFile_ButtonClick()
        {
            try
            {
                string path = Common.pointCheckEncrypterPath;
                OpenFileDialog fileDialog = new OpenFileDialog();
                fileDialog.Multiselect = true;
                fileDialog.InitialDirectory = path;
                fileDialog.Title = "请选择点检文件";
                fileDialog.Filter = "点检文件(*.bz)|*.bz";
                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    filePath = fileDialog.FileName;
                    string strEncrypt = string.Empty;
                    string strDecrypt = string.Empty;
                    string strLine = "";
                    //逐行读取并解密加密文件
                    System.Text.Encoding encoding = Encoding.GetEncoding(0);
                    System.IO.FileStream fs = new System.IO.FileStream(filePath, FileMode.Open,
                        FileAccess.Read);

                    System.IO.StreamReader sr = new System.IO.StreamReader(fs, encoding);
                    while ((strLine = sr.ReadLine()) != null && strLine != "")
                    {
                        strDecrypt += (StringEncryptUtils.Decrypt(strLine.Replace(Environment.NewLine, "")) + Environment.NewLine);
                    }
                    sr.Close();
                    fs.Close();

                    if (!Directory.Exists(Common.pointCheckEncrypterPath))
                    {
                        Directory.CreateDirectory(Common.pointCheckEncrypterPath);
                    }

                    FileUtils.AppendText(tempPath, strDecrypt);//将解密后的字符串写到临时文件

                    dt = FileUtils.OpenCSV(tempPath);//将解密内容显示到界面
                    FileUtils.DeleteFile(tempPath);
                    dgv_PointCheck.DataSource = null;
                    dgv_PointCheck.DataSource = dt;
                }
            }
            catch (Exception ex)
            {
                m_Logger.Error(ex.StackTrace);
                MessageBox.Show("操作点检文件失败！");
            }
        }

        private void btn_Confirm_ButtonClick()
        {
            try
            {
                if (string.IsNullOrEmpty(tb_Who.Text.Trim()))
                {
                    tb_Who.Text = "请输入审核人";
                    return;
                }
                if (dt == null || dt.Rows.Count == 0)
                {
                    btn_ReadPointCheckFile_ButtonClick();
                    return;
                }
                if (dt.Rows[0]["approve"] != null && dt.Rows[0]["approve"].ToString() != "")
                {
                    DialogResult dr = MessageBox.Show(this, "审核人已存在，是否修改？", "确认框", MessageBoxButtons.OKCancel);
                    if (dr == DialogResult.Cancel)
                    {
                        return;
                    }
                }
                dt.Rows[0]["approve"] = tb_Who.Text.Trim();

                FileUtils.SaveCSV(dt, tempPath);
                string strEncrypt = string.Empty;//读取修改后文件内容
                string strLine = "";
                //逐行读取修改后的解密文件
                System.Text.Encoding encoding = Encoding.GetEncoding(0);
                System.IO.FileStream fs = new System.IO.FileStream(tempPath, FileMode.Open,
                    FileAccess.Read);

                System.IO.StreamReader sr = new System.IO.StreamReader(fs, encoding);
                while ((strLine = sr.ReadLine()) != null && strLine != "")
                {
                    strEncrypt += (StringEncryptUtils.Encrypt(strLine.Replace(Environment.NewLine, "")) + Environment.NewLine);
                }
                sr.Close();
                fs.Close();
                FileUtils.DeleteFile(tempPath);
                FileUtils.ClearFile(filePath);
                FileUtils.AppendText(filePath, strEncrypt);//将加密后的内容再次写回到点检文件
            }
            catch (Exception ex)
            {
                m_Logger.Error(ex.StackTrace);
                MessageBox.Show("操作点检文件失败！");
            }
        }

        private void btn_ReadPointCheckFile_Load(object sender, EventArgs e)
        {

        }
    }
}
